package com.ibeetl.cms.web;

import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreRole;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.Message;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.MessageService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.FileUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.admin.core.web.query.MessageQuery;
import com.ibeetl.cms.util.RoleConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Message 接口
 */
@Controller
public class MessageController {

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/message";

    @Autowired
    private CorePlatformService platformService;


    @Autowired
    private MessageService messageService;

    @Autowired
    FileService fileService;
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @Function("cms.message.query")
    @ResponseBody
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/cms/message/index.html");
        view.addObject("search", MessageQuery.class.getName());
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("cms.message.edit")
    @ResponseBody
    public ModelAndView edit(Integer id) {
        ModelAndView view = new ModelAndView("/cms/message/edit.html");
        Message message = messageService.queryById(id);
        view.addObject("message", message);
        return view;
    }

    @GetMapping(MODEL + "/add.do")
    @Function("cms.message.add")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/cms/message/add.html");
        return view;
    }

    /* ajax json */

    @PostMapping(MODEL + "/list.json")
    @Function("cms.message.query")
    @ResponseBody
    public JsonResult<PageQuery> list(MessageQuery condtion) {
        PageQuery page = condtion.getPageQuery();
        messageService.queryByCondition(page);
        return JsonResult.success(page);
    }

    @PostMapping(MODEL + "/add.json")
    @Function("cms.message.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class) Message message) {
        messageService.save(message);
        return new JsonResult().success();
    }

    @PostMapping(MODEL + "/update.json")
    @Function("cms.message.update")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class) Message message) {
        boolean success = messageService.updateTemplate(message);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }


    @GetMapping(MODEL + "/view.json")
    @Function("cms.message.query")
    @ResponseBody
    public JsonResult<Message> queryInfo(Integer id) {
        Message message = messageService.queryById(id);
        return JsonResult.success(message);
    }

    @PostMapping(MODEL + "/delete.json")
    @Function("cms.message.delete")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        messageService.batchDelMessage(idList);
        return new JsonResult().success();
    }


    @RequestMapping(MODEL + "/message.do")
    public String messageIndex() {
        return "/cms/message/messages.html";
    }


    @RequestMapping(MODEL + "/help.do")
    public String helpDoc(HttpServletResponse response) throws IOException {
        response.setContentType("text/html; charset = UTF-8");
        CoreRole role = platformService.getRole();
        String file;

        if (role.getId() == RoleConstant.PERSON) {
            file = "人才申报-操作手册.pdf";
        } else if (role.getId() == RoleConstant.COMPANY) {
            file = "企业受理-操作手册.pdf";
        } else if (role.getId() == RoleConstant.FIRSTCHECK) {
            file = "初审-操作手册.pdf";
        } else if (role.getId() == RoleConstant.FINALCHECK) {
            file = "复审-操作手册.pdf";
        } else if (role.getId() == RoleConstant.HUMAN_SOCIETY) {
            file = "人社管理员-操作手册.pdf";
        } else {
            file = "人才申报-操作手册.pdf";
        }
        response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(file, "UTF-8"));
        InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("helpDoc/" + file);
        FileUtil.copy(input, response.getOutputStream());
        return null;
    }

    /**
     * 消息列表
     *
     * @return
     */
    @RequestMapping(MODEL + "/message.json")
    @ResponseBody
    public JsonResult<List<Message>> messagelist() {
        CoreUser user = platformService.getCurrentUser();
        CoreRole role = platformService.getRole();
        List<Message> result = messageService.messageList(user, role);
        return JsonResult.success(result);
    }
}
